package com.frogs.ucenter.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.PropertyUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.alibaba.fastjson.JSON;
import com.frogs.framework.base.web.BaseController3;
import com.frogs.framework.util.DWZResponse;
import com.frogs.framework.util.PageList;
import com.frogs.framework.util.RenderUtil;
import com.frogs.framework.util.StringUtil;
import com.frogs.framework.web.HttpRequestInfo;
import com.frogs.ucenter.entity.SubFlowInfo;
import com.frogs.ucenter.entity.SubFlowInfoExt;
import com.frogs.ucenter.entity.TotalFlowInfo;
import com.frogs.ucenter.entity.TotalFlowRelation;
import com.frogs.ucenter.service.SubFlowInfoService;
import com.frogs.ucenter.service.TotalFlowInfoService;

@Controller
@RequestMapping("/admin/subFlowInfo")
public class SubFlowInfoController extends BaseController3 {
    
	private static final String ROOT_VIEW = "admin/subFlowInfo";
    private static final String HOME_VIEW = "admin/subFlowInfo/home";
    private static final String EDIT_VIEW = "admin/subFlowInfo/edit";
    private static final String LIST_VIEW = "admin/subFlowInfo/list";
    
    private static final String LIST_RELATION = "/listRelation";
    private static final String LIST_RELATION_VIEW = ROOT_VIEW + LIST_RELATION;
    private static final String EDIT_RELATION = "/editRelation";
    private static final String EDIT_RELATION_VIEW = ROOT_VIEW + EDIT_RELATION;
    private static final String ADD_RELATION = "/addRelation";
    private static final String DELETE_RELATION = "/deleteRelation";

    @Resource
    private SubFlowInfoService subFlowInfoService;
    
    @Resource
    private TotalFlowInfoService totalFlowInfoService;

    @RequestMapping()
    public String home(HttpServletRequest request, ModelMap model, String isLookup) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        setRequestModelMap( request, model, true );
        getPageList( model );
        if (StringUtil.isEmpty(isLookup)) {
        	return HOME_VIEW;
        }
        else{
        	return LIST_VIEW;
        }
    }

    @RequestMapping(DELETE_RELATION)
    public String deleteRelation(HttpServletRequest request, HttpServletResponse response, ModelMap model, @RequestParam(required=true, value="ids") String inIdList) {
        setRequestModelMap(request, model);
        DWZResponse.Builder builder;
        boolean flag = true;
        try {
            String[] idArray = inIdList.split(",");
            model.put("inIdList", idArray);
            subFlowInfoService.deleteRelation( model ); 
            builder = DWZResponse.getSucessBuilder("删除成功");
        } catch (Exception e) {
        	flag = false;
            builder = DWZResponse.getFailBuilder("删除失败" + e.getMessage());
        }
        RenderUtil.renderHtml(builder.build().toString(), response);
        model.clear();
        request.setAttribute("flag", flag);
        return null;
    }

    @RequestMapping(ADD_RELATION)
    public String addSubFlowInfo(HttpServletRequest request, HttpServletResponse response, ModelMap model,TotalFlowRelation totalFlowRelation) {
    	HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        DWZResponse.Builder builder;
        boolean flag = true;
        try {
        	int flowId = totalFlowRelation.getTotalFlowId();
        	int sequence = totalFlowRelation.getSequence();
        	if(subFlowInfoService.isExistsTaskRelation(flowId, sequence)){
        		flag = false;
        		builder = DWZResponse.getFailBuilder("新增失败,已经存在该序号，请重新输入序号");
        	}else{
	            subFlowInfoService.insertRelation( totalFlowRelation); 
	            builder = DWZResponse.getSucessBuilder("新增成功");
        	}
        } catch (Exception e) {
        	flag = false;
            builder = DWZResponse.getFailBuilder("新增失败" + e.getMessage());
        }
        RenderUtil.renderHtml(builder.build().toString(), response);
        request.setAttribute("flag", flag);
        return null;
    }
    
    @RequestMapping(LIST_RELATION)
    public String listSubFlowInfo(HttpServletRequest request, ModelMap model) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        setRequestModelMap( request, model, true );
        List<SubFlowInfoExt> subFlowInfoList = subFlowInfoService.getSubFlowInfoExtList( model); 
        TotalFlowInfo totalFlowInfo = totalFlowInfoService.getById(Integer.valueOf(model.get("eqTotalFlowId").toString()));
        model.put("subFlowInfoList",subFlowInfoList);
        model.put("totalFlowInfo",totalFlowInfo);
        return LIST_RELATION_VIEW;
    }
    
    @RequestMapping(EDIT_RELATION)
    public String editRelation(HttpServletRequest request, ModelMap model) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        setRequestModelMap( request, model, false );
        Integer id = reqInfo.getIntParameter( "eqTotalFlowId", -1 );
        model.put( "model",totalFlowInfoService.getById( id ) ); 
        return EDIT_RELATION_VIEW;
    }

    @RequestMapping(EDIT)
    public String createSubFlowInfo(HttpServletRequest request, ModelMap model) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        setRequestModelMap( request, model, false );
        Integer id = reqInfo.getIntParameter( "id", -1 );
        if( id > 0 ){
            model.put( "model",subFlowInfoService.getById( id ) ); 
        }
        return EDIT_VIEW;
    }

    @RequestMapping(ADD)
    public String saveSubFlowInfo(HttpServletRequest request, HttpServletResponse response, ModelMap model, SubFlowInfo subFlowInfo) {
        setRequestModelMap(request, model, false);
        DWZResponse.Builder builder;
        boolean flag = true;
        try {
            subFlowInfoService.insert( subFlowInfo ); 
            builder = DWZResponse.getSucessBuilder("新增成功");
        } catch (Exception e) {
        	flag = false;
            builder = DWZResponse.getFailBuilder("新增失败" + e.getMessage());
        }
        RenderUtil.renderHtml(builder.build().toString(), response);
        request.setAttribute("flag", flag);
        return null;
    }

    @RequestMapping(SHOW)
    public String showSubFlowInfo(HttpServletRequest request, ModelMap model) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        Integer id = reqInfo.getIntParameter( "id", -1 );
        if( id > 0 ){
            model.addAttribute( subFlowInfoService.getById( id ) ); 
        }
        setRequestModelMap(request, model);
        return null;
    }

    @RequestMapping(UPDATE)
    public String updateSubFlowInfo(HttpServletRequest request, HttpServletResponse response, ModelMap model, SubFlowInfo subFlowInfoNew) throws Exception {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        Integer id = reqInfo.getIntParameter( "id", -1 );
        boolean flag = true;
        if( id > 0 ){
            DWZResponse.Builder builder;
            try {
                SubFlowInfo subFlowInfo = subFlowInfoService.getById( id ); 
                PropertyUtils.copyProperties(subFlowInfo, subFlowInfoNew);
                subFlowInfoService.update( subFlowInfo ); 
                model.clear();
                builder = DWZResponse.getSucessBuilder("更新成功");
            } catch (Exception e) {
            	flag = false;
                builder = DWZResponse.getFailBuilder("更新失败" + e.getMessage());
            }
            RenderUtil.renderHtml(builder.build().toString(), response);
        }
        request.setAttribute("flag", flag);
        return null;
    }

    @RequestMapping(DELETE)
    public String deleteSubFlowInfo(HttpServletRequest request, HttpServletResponse response, ModelMap model, String inIdList) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        setRequestModelMap(request, model);
        DWZResponse.Builder builder;
        boolean flag = true;
        try {
            String[] idArray = inIdList.split(",");
            model.put("inIdList", idArray);
            subFlowInfoService.delete( model ); 
            builder = DWZResponse.getSucessBuilder("删除成功");
        } catch (Exception e) {
        	flag = false;
            builder = DWZResponse.getFailBuilder("删除失败" + e.getMessage());
        }
        RenderUtil.renderHtml(builder.build().toString(), response);
        model.clear();
        request.setAttribute("flag", flag);
        return null;
    }

    private PageList<SubFlowInfo> getPageList(ModelMap model) {
        PageList<SubFlowInfo> subFlowInfoList = subFlowInfoService.getList( model, Integer.parseInt(model.get("pageNum").toString()), Integer.parseInt(model.get("numPerPage").toString()) ); 
        model.put("subFlowInfoList",subFlowInfoList);
        model.put("pageTurn",subFlowInfoList.getPageTurn());
        return subFlowInfoList;
    }
}